package org.boulderfoodrescue.webapp.client.old.viewer.scheduler;

import java.util.Comparator;
import java.util.List;

import org.boulderfoodrescue.webapp.client.pickuptimes.timepicker.PickupWindow;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.shared.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.view.client.ListDataProvider;

public class AvailableTimesCellTable extends FlowPanel{
	protected CellTable<PickupWindow> table;
	protected ListDataProvider<PickupWindow> dataProvider;
	public AvailableTimesCellTable(){
		dataProvider = new ListDataProvider<PickupWindow>();
		table = new CellTable<PickupWindow>();

		TextColumn<PickupWindow> startColumn = new TextColumn<PickupWindow>(){
			@Override
			public String getValue(PickupWindow object) {
				return DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(object.getStart_time());
			}
		};

		TextColumn<PickupWindow> endColumn = new TextColumn<PickupWindow>(){
			@Override
			public String getValue(PickupWindow object) {
				return DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(object.getEnd_time());
			}
		};

		ButtonCell removeCell = new ButtonCell();
		Column<PickupWindow, String> removeColumn = new Column<PickupWindow, String>(removeCell){
			@Override
			public String getValue(PickupWindow object) {
				return "X";
			}
		};

		table.addColumn(startColumn, "Start");
		table.addColumn(endColumn, "End");
		table.addColumn(removeColumn, "Delete");

		dataProvider.addDataDisplay(table);

		ListHandler<PickupWindow> sortHandler = new ListHandler<PickupWindow>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);
		
		startColumn.setSortable(true);
		sortHandler.setComparator(startColumn, new Comparator<PickupWindow>(){
			@Override
			public int compare(PickupWindow o1, PickupWindow o2) {
				long start1 = o1.getStart_time().getTime();
				long start2 = o2.getStart_time().getTime();
				if(start1 == start2)
					return 0;
				return (start1 > start2) ? 1 : -1;
			}
		});

		endColumn.setSortable(true);
		sortHandler.setComparator(endColumn, new Comparator<PickupWindow>(){
			@Override
			public int compare(PickupWindow o1, PickupWindow o2) {
				long start1 = o1.getStart_time().getTime();
				long start2 = o2.getStart_time().getTime();
				if(start1 == start2)
					return 0;
				return (start1 > start2) ? 1 : -1;
			}
		});

		removeColumn.setFieldUpdater(new FieldUpdater<PickupWindow, String>(){
      @Override
      public void update(int index, PickupWindow object, String value) {
      	removePickupEvent(object);
      }
    });
		
		this.add(table);
		table.setWidth("100%");
	}
	public void addPickupEvent(PickupWindow time_interval){
		if(time_interval != null)
			dataProvider.getList().add(time_interval);
	}
	public void removePickupEvent(PickupWindow time_interval){
		dataProvider.getList().remove(time_interval);
	}
	public List<PickupWindow> getPickupTimes(){
		return dataProvider.getList();
	}
}
